import {defineStore} from "pinia";
import dayjs from "@/utils/dayjs";

export const useAppStore = defineStore('app', {
    persist: true, //使用持久化插件
    state: () => {   //是一个函数，返回一个对象，用于存储状态
        return {
            theme: 'light',
            previewTheme: 'default',
            codeTheme: 'atom',
            authModalShow: false,
            aiModalStatus: 'none',
            boardModalStatus: 'none',
            topModalName: '',
            emailGetCodeTime: 1692693110,
            searchKeyHistory: [] as string[],


            openPostPreview: true,
            postShowMode:'list'

        }
    },
    getters: {},  //用于存储计算属性
    actions: {  //用于封装业务逻辑
        startEmailCode() {
            this.emailGetCodeTime = dayjs().unix()
        },
        searchKey(key: string) {
            let index = this.searchKeyHistory.findIndex(item => item === key)
            if (index !== -1) {
                this.searchKeyHistory.splice(index, 1)
            }
            this.searchKeyHistory.unshift(key)
            this.searchKeyHistory = this.searchKeyHistory.slice(0, 10)
        },
        removeKey(key: string) {
            let index = this.searchKeyHistory.findIndex(item => item === key)
            if (index !== -1) {
                this.searchKeyHistory.splice(index, 1)
            }
        },
        clearKey() {
            this.searchKeyHistory = []
        }

    },
})